package com.tudouji.project.system.service.impl;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.tudouji.project.system.mapper.BusinesscodeMapper;
import com.tudouji.project.system.domain.Businesscode;
import com.tudouji.project.system.service.IBusinesscodeService;

/**
 * 业务编码Service业务层处理
 * 
 * @author tudouji
 * @date 2021-10-27
 */
@Service
public class BusinesscodeServiceImpl implements IBusinesscodeService 
{
    @Autowired
    private BusinesscodeMapper businesscodeMapper;

    /**
     * 查询业务编码
     * 
     * @param guid 业务编码ID
     * @return 业务编码
     */
    @Override
    public Businesscode selectBusinesscodeById(String guid)
    {
        return businesscodeMapper.selectBusinesscodeById(guid);
    }

    /**
     * 查询业务编码列表
     * 
     * @param businesscode 业务编码
     * @return 业务编码
     */
    @Override
    public List<Businesscode> selectBusinesscodeList(Businesscode businesscode)
    {
        return businesscodeMapper.selectBusinesscodeList(businesscode);
    }

    /**
     * 新增业务编码
     * 
     * @param businesscode 业务编码
     * @return 结果
     */
    @Override
    public int insertBusinesscode(Businesscode businesscode)
    {
        return businesscodeMapper.insertBusinesscode(businesscode);
    }

    /**
     * 修改业务编码
     * 
     * @param businesscode 业务编码
     * @return 结果
     */
    @Override
    public int updateBusinesscode(Businesscode businesscode)
    {
        return businesscodeMapper.updateBusinesscode(businesscode);
    }

    /**
     * 批量删除业务编码
     * 
     * @param guids 需要删除的业务编码ID
     * @return 结果
     */
    @Override
    public int deleteBusinesscodeByIds(String[] guids)
    {
        return businesscodeMapper.deleteBusinesscodeByIds(guids);
    }

    /**
     * 删除业务编码信息
     * 
     * @param guid 业务编码ID
     * @return 结果
     */
    @Override
    public int deleteBusinesscodeById(String guid)
    {
        return businesscodeMapper.deleteBusinesscodeById(guid);
    }

    /**
     * 通过id获取业务编码
     * @param id
     * @throws Exception
     */
    @Override
    public String getCode(String id){
        Businesscode businessCode=businesscodeMapper.selectBusinesscodeById(id);
        String code="";
        Calendar now = Calendar.getInstance();

        Calendar calendar = Calendar.getInstance();
        calendar.setTime(businessCode.getCreatedate());

        SimpleDateFormat formate=new SimpleDateFormat("yyMM");
        String date=formate.format(new Date());
        if(now.get(Calendar.YEAR)==calendar.get(Calendar.YEAR)&&now.get(Calendar.MONTH)==calendar.get(Calendar.MONTH)){
            code=businessCode.getPrefix().concat(date).concat(businessCode.getSequence()+"");
            //更新序号
            Businesscode bc=new Businesscode();
            bc.setGuid(id);
            bc.setSequence(businessCode.getSequence()+1);
            businesscodeMapper.updateBusinesscode(bc);
        }else{
            code=businessCode.getPrefix().concat(date).concat("1");
            Businesscode bc=new Businesscode();
            bc.setGuid(id);
            bc.setCreatedate(new Date());
            bc.setSequence(2);
            businesscodeMapper.updateBusinesscode(bc);
        }
        return code;
    }
}
